package com.manage.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.manage.modules.utils.DateUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.manage.modules.bo.TBaseStationStateYunAddBo;
import com.manage.modules.bo.TBaseStationStateYunQueryBo;
import com.manage.modules.bo.TBaseStationStateYunEditBo;
import com.manage.modules.domain.TBaseStationStateYun;
import com.manage.modules.mapper.TBaseStationStateYunMapper;
import com.manage.modules.vo.TBaseStationStateYunVo;
import com.manage.modules.service.ITBaseStationStateYunService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 站点状态列表Service业务层处理
 *
 * @author wx
 * @date 2021-09-07
 */
@Service
public class TBaseStationStateYunServiceImpl extends ServiceImpl<TBaseStationStateYunMapper, TBaseStationStateYun> implements ITBaseStationStateYunService {
    @Resource
    private TBaseStationStateYunMapper stationStateYunMapper;

    @Override
    public void postOverStationState(List<TBaseStationStateYun> stateYunList) {
        if (stateYunList == null || stateYunList.size() == 0) {
            return;
        }
        for (TBaseStationStateYun stateYun : stateYunList) {
            LambdaQueryWrapper<TBaseStationStateYun> lqw = Wrappers.lambdaQuery();
            lqw
                    .eq(true, TBaseStationStateYun::getRequestNum, stateYun.getRequestNum())
                    .eq(true, TBaseStationStateYun::getStationIdC, stateYun.getStationIdC());
            stateYun.setId(getOne(lqw).getId());
        }
        updateBatchById(stateYunList);
    }

    @Override
    public List<TBaseStationStateYunVo> listMap() {
        // 查询每个站点最新一条
        List<TBaseStationStateYun> yunList = stationStateYunMapper.selectNewGroupByStationIdc();
        List<TBaseStationStateYunVo> voList = new ArrayList<>();
        for (TBaseStationStateYun stateYun : yunList) {
            TBaseStationStateYunVo vo = BeanUtil.toBean(stateYun, TBaseStationStateYunVo.class);
            // 去掉以 00 的开头
            if (vo.getCollectorMainboardVoltage()!=null&&vo.getCollectorMainboardVoltage().startsWith("00")) {
                vo.setCollectorMainboardVoltage(vo.getCollectorMainboardVoltage().replace("00", ""));
            }
            LocalDateTime newDateTime = DateUtil.dateTimeToLocal(stateYun.getCreateTime());
            // 上传的时间与现在的时间比较
            long betweenHour = DateUtil.betweenHours(newDateTime, LocalDateTime.now());
            // 如果超过1个小时未上传 便不是最新的
            if (betweenHour > 1) {
                vo.setIsNew("2");
            } else {
                vo.setIsNew("1");
            }

            voList.add(vo);
        }
        return voList;
    }

    @Override
    public TBaseStationStateYunVo queryById(Long id) {
        TBaseStationStateYun db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, TBaseStationStateYunVo.class);
    }

    @Override
    public List<TBaseStationStateYunVo> queryList(TBaseStationStateYunQueryBo bo) {
        LambdaQueryWrapper<TBaseStationStateYun> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getStationIdC()), TBaseStationStateYun::getStationIdC, bo.getStationIdC());
        lqw.eq(bo.getReceiveTime() != null, TBaseStationStateYun::getReceiveTime, bo.getReceiveTime());
        lqw.eq(StrUtil.isNotBlank(bo.getStateType()), TBaseStationStateYun::getStateType, bo.getStateType());
        lqw.eq(StrUtil.isNotBlank(bo.getIsNormalComWithSubCommunicationState()), TBaseStationStateYun::getIsNormalComWithSubCommunicationState, bo.getIsNormalComWithSubCommunicationState());
        lqw.orderByDesc(TBaseStationStateYun::getId);
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<TBaseStationStateYunVo> entity2Vo(Collection<TBaseStationStateYun> collection) {
        List<TBaseStationStateYunVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, TBaseStationStateYunVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<TBaseStationStateYun> page = (Page<TBaseStationStateYun>) collection;
            Page<TBaseStationStateYunVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(TBaseStationStateYunAddBo bo) {
        TBaseStationStateYun add = BeanUtil.toBean(bo, TBaseStationStateYun.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(TBaseStationStateYunEditBo bo) {
        TBaseStationStateYun update = BeanUtil.toBean(bo, TBaseStationStateYun.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TBaseStationStateYun entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
